package com.itheima.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/*
    加载配置文件，最终得到SqlSession
 */
public class MyBatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static{
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  获得SqlSession对象
     */
    public static SqlSession openSession(){
        // 从工厂中得到一个SqlSession
        return sqlSessionFactory.openSession();
    }

}
